﻿using AutoMapper;
using ManagementApi.Authorizers;
using ManagementApi.Models;
using ManagementApi.Models.Person;
using ManagementApi.Models.Project;
using ManagementApi.Mvc;
using ManagementApi.Mvc.ApplicationContexts;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Modules.Attachment;
using Modules.Project;
using static Modules.Project.ProjectLockLogConstants;

namespace ManagementApi.Controllers.CorpPannel
{
    /// <summary>
    /// 项目解上锁记录
    /// </summary>
    [Route("/api/v1/corp/projectLockLog")]
    [ApiController]
    [Authorize(Roles = "Corp")]
    public class ProjectLockLogController : ControllerBase
    {
        private readonly CorpApplicationContext applicationContext;
        private readonly IMapper mapper;
        private readonly ProjectLockLogService projectLockLogService;
        private readonly ProjectAttachmentService projectAttachmentService;
        private readonly ProjectCorpService projectCorpService;
        private readonly ProjectOwnAuthorizer ownAuthorizer;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="applicationContext">上下文</param>
        /// <param name="mapper">mapper映射</param>
        /// <param name="projectLockLogService">主业务service</param>
        /// <param name="projectAttachmentService">附件service</param>
        public ProjectLockLogController(CorpApplicationContext applicationContext, IMapper mapper, ProjectLockLogService projectLockLogService, ProjectAttachmentService projectAttachmentService, ProjectCorpService projectCorpService, ProjectOwnAuthorizer ownAuthorizer)
        {
            this.applicationContext = applicationContext;
            this.mapper = mapper;
            this.projectLockLogService = projectLockLogService;
            this.projectAttachmentService = projectAttachmentService;
            this.projectCorpService = projectCorpService;
            this.ownAuthorizer = ownAuthorizer;
        }

        /***进入模版管理可以修改模版***/

        /// <summary>
        /// 获取项目解上锁记录列表(分页)
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <param name="pageNum">页码</param>
        /// <param name="pageSize">每页数量</param>
        /// <returns></returns>
        [HttpGet("page")]
        public ApiResult<PageDto<ProjectLockLogDto>> GetProjectLockLogs([FromQuery] ProjectLockLogQuery query, int pageNum = 1, int pageSize = 20)
        {
            query.ProjectId = applicationContext.CurrentProjectInfo.ProjectId;
            var data = projectLockLogService.GetProjectLockLogs(query, pageNum, pageSize);
            var dtos = data.AsPageDto(source => { return source.AsDto(); });
            return ApiResult<PageDto<ProjectLockLogDto>>.Success(dtos);
        }

        /// <summary>
        /// 获取项目解上锁记录列表(分页) 复核
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <param name="pageNum">页码</param>
        /// <param name="pageSize">每页数量</param>
        /// <returns></returns>
        [HttpGet("page_fh")]
        public ApiResult<PageDto<ProjectLockLogDto>> GetProjectLockLogsBySours([FromQuery] ProjectLockLogQuery query, int pageNum = 1, int pageSize = 20)
        {
            query.ProjectId = applicationContext.CurrentProjectInfo.ProjectId;
            query.DataSourse = 解锁数据来源.手动;
            var data = projectLockLogService.GetProjectLockLogs(query, pageNum, pageSize);
            var dtos = data.AsPageDto(source => { return source.AsDto(); });
            return ApiResult<PageDto<ProjectLockLogDto>>.Success(dtos);
        }

        /// <summary>
        /// 获取项目解上锁记录列表(分页) 用于复核的时候选择上锁记录
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <param name="pageNum">页码</param>
        /// <param name="pageSize">每页数量</param>
        /// <returns></returns>
        [HttpGet("page_pull_down")]
        public ApiResult<PageDto<ProjectLockLogDto>> GetProjectLockLogPullDown([FromQuery] ProjectLockLogQuery query, int pageNum = 1, int pageSize = 20)
        {
            query.ProjectId = applicationContext.CurrentProjectInfo.ProjectId;
            query.HasLocked = true;
            var data = projectLockLogService.GetProjectLockLogsByLock(query, pageNum, pageSize);
            var dtos = data.AsPageDto(source => { return source.AsDto(); });
            return ApiResult<PageDto<ProjectLockLogDto>>.Success(dtos);
        }

        /// <summary>
        /// 项目解上锁记录信息表单数据
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>
        [HttpGet("{id}/form")]
        public ApiResult<ProjectLockLogEditForm> GetRole(int id)
        {
            var data = projectLockLogService.Get(id);
            if (data == null)
                return ApiResult<ProjectLockLogEditForm>.Failure("项目解上锁记录信息数据不存在");
            ProjectLockLogEditForm form = data.AsForm();

            #region 附件查询

            var attachments = projectAttachmentService.GetSysAttachmentByObjectIdAndType(data.Id, 附件类型.历史状态复核证明, 项目附件租户.ProjectLockLog);
            List<AttachmentsForm<int>> attach = new();
            foreach (var item in attachments)
            {
                AttachmentsForm<int> attachmentsForm = new()
                {
                    AttachmentId = item.Id,
                    AttachmentName = item.FriendlyName,
                };
                attach.Add(attachmentsForm);
            }
            form.Attachments = attach;

            #endregion 附件查询

            return ApiResult<ProjectLockLogEditForm>.Success(form);
        }

        /// <summary>
        /// 历史状态复核申请
        /// </summary>
        /// <param name="id">id</param>
        /// <param name="form">表单信息</param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public ApiResult Update(int id, [FromBody] ProjectLockLogEditForm form)
        {
            var data = projectLockLogService.Get(id);
            if (data == null) return ApiResult.Failure("项目解上锁记录信息不存在");
            ownAuthorizer.CheckAndThrowIfOwnAuthenticationFailed(data.ProjectId);
            projectLockLogService.Verify(data);
            data.UnlockReason = form.UnlockReason;
            data.DataSourse = 解锁数据来源.手动;
            data.LastUpdateTime = DateTime.Now;
            data.AuditStatus = 解上锁审核状态.待审核;
            data.UnlockApplicant = applicationContext.CurrentUser?.Name ?? string.Empty;
            data.UnlockApplicationTime = DateTime.Now;
            data.HasPush = false;
            projectLockLogService.Update(data, form.Attachments?.Select(a => a.AttachmentId));
            return ApiResult.Success("申请成功");
        }

    }
}